package com.gs.zsmanage.utils;

import com.gs.zsmanage.entity.Alarm;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.ArrayList;
import java.util.List;

import static com.gs.zsmanage.utils.Consts.AREACODES;
import static com.gs.zsmanage.utils.Consts.AREANAMES;

public class XmlParse {

    public static List<Alarm> getAlarms() throws Exception {

        SAXReader reader = new SAXReader();

        Document document = reader.read("http://www.weather.com.cn/data/alarm_xml/alarminfo.xml");
        Element root = document.getRootElement();
        List<Element> childElements = root.elements();
        List<Alarm> alarms = new ArrayList<>();
        Alarm alarm;
        for (String areaName : AREANAMES) {
            for (Element child : childElements) {
                List<Element> secChildElements = child.elements("Station");
                for (Element secChild : secChildElements) {
                    if (AREACODES.get(areaName).equals(secChild.attributeValue("areaId"))) {
                        alarm = new Alarm();
                        alarm.setStationName(areaName);
                        alarm.setAreaId(AREACODES.get(areaName));
                        alarm.setLat(secChild.attributeValue("lat"));
                        alarm.setLon(secChild.attributeValue("lon"));
                        alarm.setSignalType(secChild.attributeValue("signalType"));
                        alarm.setSignalLevel(secChild.attributeValue("signalLevel"));
                        alarm.setIssueTime(secChild.attributeValue("issueTime"));
                        alarm.setIssueContent(secChild.attributeValue("issueContent"));
                        alarm.setRelieveTime(secChild.attributeValue("relieveTime"));
                        alarms.add(alarm);
                    }
                }
            }
        }

        return alarms;
    }


}
